package util{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	
	import model.EventTDir;
	import model.siw.RuleFigure;
	
	import mx.collections.ArrayList;
	
	
	public class ConfigUtil{
		
		private var xml:XML;
		
		private var processId:String;
		
		private var processName:String;
		
		private var packagePath:String;
		
		private var activityId:String;
		
		private var activityName:String;
		
		private var processUrl:String="";
		
		private var consoleUrl:String="";
		
		private var processDownloadUrl:String="";
		
		private var eventDirUri:String="";
		
		private var bsipUri:String="";
		
		private var currentEventT:EventTDir;
		
		private var rules:ArrayList;
		
		public function ConfigUtil(){
			
			initConfig();
			
			rules=new ArrayList();
			
		}
		
		private function initConfig():void{
			
			var loader:URLLoader=new URLLoader();
			loader.dataFormat=URLLoaderDataFormat.TEXT;
			loader.addEventListener(Event.COMPLETE, handleXMLLoaderComplete);
			loader.load(new URLRequest(Constants.CONFIG_PATH));
			
		}
		
		public function handleXMLLoaderComplete(event:Event):void{
          	 
          	xml=new XML(event.target.data);
          	
  		}
  		
  		/* public function getProcessURL():String{
  			if(processUrl==""){
  				var i:int;
  				for(i=0;i<xml.children().length();i++){
  					if(new XML(xml.children()[i]).attribute("name").toString()=="Server"){
  						var j:int;
  						for(j=0;j<new XML(xml.children()[i]).children().length();j++){
  							if(new XML(new XML(xml.children()[i]).children()[j]).attribute("name").toString()=="ProcessManager"){
  								processUrl=new XML(new XML(xml.children()[i]).children()[j]).attribute("value").toString();
  								return processUrl;
  							}
  						}
  					}
  				}
  			}else{
  				
  				return processUrl;
  				
  			}
  			
  			return processUrl;
  		} */
  		
  		public function getProcessURL():String{
  			if(processUrl==""){
  				var i:int;
  				for(i=0;i<xml.children().length();i++){
  					if(new XML(xml.children()[i]).attribute("name").toString()=="Server"){
  						var j:int;
  						for(j=0;j<new XML(xml.children()[i]).children().length();j++){
  							if(new XML(new XML(xml.children()[i]).children()[j]).attribute("name").toString()=="ProcessManager"){
  								processUrl=new XML(new XML(xml.children()[i]).children()[j]).attribute("value").toString();
  								return processUrl;
  							}
  						}
  					}
  				}
  			}else{
  				
  				return processUrl;
  				
  			}
  			
  			return processUrl;
  		}
  		
  		public function getBsipURI():String{
  			if(bsipUri==""){
  				var i:int;
  				for(i=0;i<xml.children().length();i++){
  					if(new XML(xml.children()[i]).attribute("name").toString()=="EVENT"){
  						var j:int;
  						for(j=0;j<new XML(xml.children()[i]).children().length();j++){
  							if(new XML(new XML(xml.children()[i]).children()[j]).attribute("name").toString()=="BSIP_XML"){
  								bsipUri=new XML(new XML(xml.children()[i]).children()[j]).attribute("value").toString();
  								return bsipUri;
  							}
  						}
  					}
  				}
  			}else{
  				
  				return bsipUri;
  				
  			}
  			
  			return processDownloadUrl;
  		}
  		
  		public function getConsoleURL():String{
  			if(consoleUrl==""){
  				var i:int;
  				for(i=0;i<xml.children().length();i++){
  					if(new XML(xml.children()[i]).attribute("name").toString()=="Server"){
  						var j:int;
  						for(j=0;j<new XML(xml.children()[i]).children().length();j++){
  							if(new XML(new XML(xml.children()[i]).children()[j]).attribute("name").toString()=="GridwfConsole"){
  								processDownloadUrl=new XML(new XML(xml.children()[i]).children()[j]).attribute("value").toString();
  								return processDownloadUrl;
  							}
  						}
  					}
  				}
  			}else{
  				
  				return consoleUrl;
  				
  			}
  			
  			return consoleUrl;
  		}
  		
  		public function getEventDirURI():String{
  			if(eventDirUri==""){
  				var i:int;
  				for(i=0;i<xml.children().length();i++){
  					if(new XML(xml.children()[i]).attribute("name").toString()=="MENU"){
  						var j:int;
  						for(j=0;j<new XML(xml.children()[i]).children().length();j++){
  							if(new XML(new XML(xml.children()[i]).children()[j]).attribute("name").toString()=="ETDIR_XML"){
  								eventDirUri=new XML(new XML(xml.children()[i]).children()[j]).attribute("value").toString();
  								return eventDirUri;
  							}
  						}
  					}
  				}
  			}else{
  				
  				return eventDirUri;
  				
  			}
  			
  			return eventDirUri;
  		}
  		
  		public function saveBsip(v:String){
  		
  		}  		
  		
  		public function getPackagePath():String{
           		return this.packagePath;
        }
        
        public function setPackagePath(v:String):void{
           		this.packagePath=v;
        }
  		
  		public function getProcessId():String{
           		return this.processId;
        }
        
        public function setProcessId(v:String):void{
           		this.processId=v;
        }
        
        public function getProcessName():String{
           		return this.processName;
        }
        
        public function setProcessName(v:String):void{
           		this.processName=v;
        }
        
        public function getActivityId():String{
           		return this.activityId;
        }
        
        public function setActivityId(v:String):void{
           		this.activityId=v;
        }
        
        public function getActivityName():String{
           		return this.activityName;
        }
        
        public function setActivityName(v:String):void{
           		this.activityName=v;
        }
        
        public function getEventT():EventTDir{
           		return this.currentEventT;
        }
        
        public function setEventT(v:EventTDir):void{
           		this.currentEventT=v;
        }
        
        public function addRule(ruleFigure:RuleFigure){
        	this.rules.addItem(ruleFigure);
        }
        
        public function removeRule(ruleFigure:RuleFigure){
        	this.rules.removeItem(ruleFigure);
        }
        
        

	}
}